package com.apache.log.service.impl;

import com.apache.api.vo.ParamsVo;
import com.apache.audit.entity.LogModel;
import com.apache.audit.entity.LogModelDetail;
import com.apache.audit.entity.LogOperation;
import com.apache.audit.entity.LogOperationDetail;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.log.db.DbBaseDao;
import com.apache.log.manager.LogManager;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.tools.UUIDUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Hou Dayu
 * @date: Created in 2020/11/18
 */
public class DBManagerImpl implements LogManager {

    private DbBaseDao dbBaseDao;

    public DBManagerImpl(DbBaseDao dbBaseDao){
        this.dbBaseDao = dbBaseDao;
    }

    @Override
    public String saveInfo(ParamsVo vo) throws BusinessException {
        Map<String,Object> data = new HashMap();
        String infoId = UUIDUtil.getPrimaryKey();
        if(vo.getObj() instanceof LogModel){
            LogModel logModel = (LogModel)vo.getObj();
            logModel.setModelId(infoId);
            data.put("value",logModel);
        } else if(vo.getObj() instanceof LogModelDetail){
            LogModelDetail detail = (LogModelDetail)vo.getObj();
            detail.setDetailId(infoId);
            data.put("value",detail);
        } else {
            LogOperation operation = (LogOperation)vo.getObj();
            operation.setLogId(infoId);
            data.put("value",operation);
            data.put("details",vo.getObjs());
        }
        boolean mark = dbBaseDao.insert(data);
        if(mark){
            return infoId;
        }
        return "";
    }

    @Override
    public boolean editInfo(ParamsVo vo) throws BusinessException {
        Map<String,Object> data = new HashMap();
        if(vo.getObj() instanceof LogModel){
            LogModel logModel = (LogModel)vo.getObj();
            if(StrUtil.isNotNull(logModel.getModelId())) {
                data.put("value", logModel);
            }
        } else if(vo.getObj() instanceof LogModelDetail){
            LogModelDetail detail = (LogModelDetail)vo.getObj();
            if(StrUtil.isNotNull(detail.getDetailId())) {
                data.put("value", detail);
            }
        }
        if(!data.isEmpty()){
            return dbBaseDao.edit(data);
        }
        return false;
    }

    @Override
    public boolean deleteInfo(ParamsVo vo) throws BusinessException {
        String infoId = vo.getInfoId();
        if (Validator.isNull(infoId)) {
            return false;
        }
        Map<String,Object> data = new HashMap();
        String sql = null;
        if(vo.getObj() instanceof LogModel){
            sql = "DELETE FROM uct_log_model where model_id='"+infoId+"'";
        } else if(vo.getObj() instanceof LogModelDetail){
            sql = "DELETE FROM uct_log_model_detail where detail_id='"+infoId+"'";
        }
        data.put("sqlStr",sql);
        return dbBaseDao.delete(data);
    }

    @Override
    public Object getInfoById(ParamsVo vo) {
        String infoId = vo.getInfoId();
        if (Validator.isNull(infoId)) {
            return false;
        }
        Map<String,Object> data = new HashMap();
        String sql = null;
        if(vo.getObj() instanceof LogModel){
            sql = "select * FROM uct_log_model where model_id='"+infoId+"'";
        } else if(vo.getObj() instanceof LogModelDetail){
            sql = "select * FROM uct_log_model_detail where detail_id='"+infoId+"'";
        } else if(vo.getObj() instanceof LogOperation){
            sql = "select * FROM uct_log_operation where log_id='"+infoId+"'";
        } else if(vo.getObj() instanceof LogOperationDetail){
            sql = "select * FROM uct_log_operation_detail where detail_id='"+infoId+"'";
        }
        data.put("sqlStr",sql);
        return dbBaseDao.selectById(data);
    }

    @Override
    public List getList(ParamsVo vo) {
        Map<String,Object> data = new HashMap();
        StringBuilder sql = new StringBuilder("select * from ");
        String str = setMethodParams(vo.getObj(),vo.getParams());
        if(StrUtil.isNull(str)){
            return new ArrayList(1);
        }
        String orderby = vo.getParamsToStr("orderBy");
        data.put("orderby",orderby);
        data.put("sqlStr",sql);
        return dbBaseDao.select(data);
    }

    @Override
    public Page getPageInfo(ParamsVo vo) {
        int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
        int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
        StringBuilder sql = new StringBuilder("select * from ");
        String str = setMethodParams(vo.getObj(),vo.getParams());
        if(StrUtil.isNull(str)){
            return new Page();
        }
        Map<String,Object> data = new HashMap();
        String orderby = vo.getParamsToStr("orderBy");
        data.put("orderby",orderby);
        data.put("sqlStr",sql);
        return dbBaseDao.pageSelect(data,pageIndex,pageSize);
    }

    private String setMethodParams(Object obj,Map<String,Object> params){
        StringBuilder sql = new StringBuilder();
        if(obj instanceof LogModel){
            sql.append(" uct_log_model where 1=1 ");
            if (!Validator.isEmpty(obj)) {
                LogModel logModel = (LogModel)obj;
                if (Validator.isNotNull(logModel.getModelCname())) {
                    sql.append(" and model_cname like '%"+logModel.getModelCname()+"%'");
                }
                if (Validator.isNotNull(logModel.getModelEname())) {
                    sql.append(" and model_ename like '%" + logModel.getModelEname() + "%'");
                }
                if (Validator.isNotNull(logModel.getModelBussiness())) {
                    sql.append(" and model_bussiness = '"+logModel.getModelBussiness()+"'");
                }
                if (Validator.isNotNull(logModel.getModelSysname())) {
                    sql.append(" and model_sysname = '"+logModel.getModelSysname()+"'");
                }
            }
            params.put("orderBy"," model_ename desc");
        } else if(obj instanceof LogModelDetail){
            sql.append(" uct_log_model_detail where 1=1 ");
            if (!Validator.isEmpty(obj)) {
                LogModelDetail logModelDetail = (LogModelDetail)obj;
                if (Validator.isNotNull(logModelDetail.getModelId())) {
                    sql.append(" and model_id = '"+logModelDetail.getModelId()+"'");
                }
                if (Validator.isNotNull(logModelDetail.getColumnCname())) {
                    sql.append(" and column_cname like '%"+logModelDetail.getColumnCname()+"%'");
                }
                if (Validator.isNotNull(logModelDetail.getColumnEname())) {
                    sql.append(" and column_cname = '"+logModelDetail.getColumnEname()+"'");
                }
            }
            params.put("orderBy"," column_cname desc");
        } else if(obj instanceof LogOperation){
            sql.append(" uct_log_operation ,(select model_id as id,model_cname,model_bussiness,model_sysname from uct_log_model) mt model_id = id ");
            if (!Validator.isEmpty(obj)) {
                LogOperation logOperation = (LogOperation)obj;
                if (Validator.isNotNull(logOperation.getModelId())) {
                    sql.append(" and model_id = '"+logOperation.getModelId()+"'");
                }
                if (Validator.isNotNull(logOperation.getLogType())) {
                    sql.append(" and log_type = '"+logOperation.getLogType()+"'");
                }
                if (Validator.isNotNull(logOperation.getCreateUser())) {
                    sql.append(" and create_user = '"+logOperation.getCreateUser()+"'");
                }
                if (Validator.isNotNull(logOperation.getLogIp())) {
                    sql.append(" and log_ip = '"+logOperation.getLogIp()+"'");
                }
                if (Validator.isNotNull(logOperation.getPrimaryKey())) {
                    sql.append(" and primary_key = '"+logOperation.getPrimaryKey()+"'");
                }
                String modelSysname = String.valueOf(params.get("modelSysname"));
                String modelBussiness = String.valueOf(params.get("modelBussiness"));
                String modelCname = String.valueOf(params.get("modelCname"));
                String createdTime = String.valueOf(params.get("createdTime"));
                if (Validator.isNotNull(modelSysname)) {
                    sql.append(" and model_sysname = '"+modelSysname+"'");
                }
                if (Validator.isNotNull(modelBussiness)) {
                    sql.append(" and model_bussiness = '"+modelBussiness+"'");
                }
                if (Validator.isNotNull(modelCname)) {
                    sql.append(" and model_cname like '%"+modelCname+"%'");
                }
                if (Validator.isNotNull(createdTime)) {
                    long startTime = DateUtils.strToLong(createdTime + " 00:00:00", DateUtils.FmtStr.yyyyMMdd_HHmmss);
                    long stopTime = DateUtils.strToLong(createdTime + " 23:59:59", DateUtils.FmtStr.yyyyMMdd_HHmmss);
                    sql.append(" and created_time BETWEEN " + startTime + " AND " + stopTime);
                }
            }
            params.put("orderBy"," created_time desc");
        } else if(obj instanceof LogOperationDetail){
            sql.append(" uct_log_operation_detail where 1=1 ");
            if (!Validator.isEmpty(obj)) {
                LogOperationDetail detail = (LogOperationDetail)obj;
                if (Validator.isNotNull(detail.getLogId())) {
                    sql.append(" and log_id = '"+detail.getLogId()+"'");
                }
                if (Validator.isNotNull(detail.getPropertyValue())) {
                    sql.append(" and property_ename = '"+detail.getPropertyEname()+"'");
                }
                String primaryKey = "000";
                if (Validator.isNotNull(detail.getPrimaryKey())) {
                    primaryKey = detail.getPrimaryKey();
                }
                sql.append(" and primary_key = '"+primaryKey+"'");
            }
            params.put("orderBy"," property_ename desc");
        }
        return sql.toString();
    }
}
